package com.zyj.myapplication.Activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.zyj.myapplication.Fragment.ClubFragment;
import com.zyj.myapplication.Fragment.HomeFragment;
import com.zyj.myapplication.Fragment.MyFragment;
import com.zyj.myapplication.Fragment.ShaituFragment;
import com.zyj.myapplication.Fragment.TrainFragment;
import com.zyj.myapplication.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;
    private LinearLayout mTabHome;
    private LinearLayout mTabShaitu;
    private LinearLayout mTabMy;

    private ImageButton mImgTrain;
    private ImageButton mImgClub;
    private ImageButton mImgHome;
    private ImageButton mImgShaitu;
    private ImageButton mImgMy;

    private Fragment Train;
    private Fragment Club;
    private Fragment Home;
    private Fragment Shaitu;
    private Fragment My;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvent();//初始化事件
        setSelect(0);//点击事件，管理Fragment,修改图片背景色

    }

    private void initEvent() {
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
        mTabShaitu.setOnClickListener(this);
        mTabMy.setOnClickListener(this);
    }

    private void initView() {
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabShaitu = (LinearLayout) findViewById(R.id.id_tab_shaitu);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgTrain = (ImageButton) findViewById(R.id.id_tab_train_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgShaitu = (ImageButton) findViewById(R.id.id_tab_shaitu_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_my_img);


    }

    //两件事：
    private void setSelect(int i) {
// 一、设置切换内容区域
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);


//二、把图片设置为亮的
        switch (i) {
            case 0:
                if (Train == null) {
                    Train = new TrainFragment();
                    transaction.add(R.id.id_content, Train);
                } else {
                    transaction.show(Train);
                }
                mImgTrain.setImageResource(R.drawable.ic_p2);
                break;
            case 1:
                if (Club == null) {
                    Club = new ClubFragment()
                    ;
                    transaction.add(R.id.id_content, Club);
                } else {
                    transaction.show(Club);

                }
                mImgClub.setImageResource(R.drawable.ic_c2);
                break;
            case 2:
                if (Home == null) {
                    Home = new HomeFragment();
                    transaction.add(R.id.id_content, Home);
                } else {
                    transaction.show(Home);
                }
                mImgHome.setImageResource(R.drawable.ic_z2);
                break;
            case 3:
                if (Shaitu == null) {
                    Shaitu = new ShaituFragment();
                    transaction.add(R.id.id_content, Shaitu);
                } else {
                    transaction.show(Shaitu);
                }
                mImgShaitu.setImageResource(R.drawable.ic_s2);
                break;

            case 4:
                if (My == null) {
                    My = new MyFragment();
                    transaction.add(R.id.id_content, My);
                } else {
                    transaction.show(My);
                }
                mImgMy.setImageResource(R.drawable.ic_m2);
                break;

            default:
                break;
        }


        transaction.commit();

    }

    private void hideFragment(FragmentTransaction transaction) {
        if (Train != null) {
            transaction.hide(Train);
        }
        if (Club != null) {
            transaction.hide(Club);
        }
        if (Home != null) {
            transaction.hide(Home);
        }
        if (Shaitu != null) {
            transaction.hide(Shaitu);
        }
        if (My != null) {
            transaction.hide(My);
        }
    }

    @Override
    public void onClick(View v) {

        resetImgs();//切换图片的控件
        switch (v.getId()) {
            case R.id.id_tab_train:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_home:
                setSelect(2);
                break;
            case R.id.id_tab_shaitu:
                setSelect(3);
                break;
            case R.id.id_tab_my:
                setSelect(4);
                break;
            default:
                break;
        }
    }

    //换颜色
    private void resetImgs() {
        mImgTrain.setImageResource(R.drawable.ic_p1);
        mImgClub.setImageResource(R.drawable.ic_c1);
        mImgHome.setImageResource(R.drawable.ic_z1);
        mImgShaitu.setImageResource(R.drawable.ic_s1);
        mImgMy.setImageResource(R.drawable.ic_m1);
    }
}

